/**
 * @author liuxin
 * @e-mail lxtobe5@gmail.com
 * @update 2012.4.7
 */
package API_sample
{
	import com.lx.utils.HashMap;
	
	import flash.display.Sprite;
	import flash.net.LocalConnection;
	import flash.system.System;
	import flash.utils.Dictionary;
	import flash.utils.describeType;
	import flash.utils.getQualifiedClassName;

	public class Dictionary_sample extends Sprite
	{
		private var dict:Dictionary;
		private var obj:Object;
		private var arr:Array;
		private var s:String;
		
		public function Dictionary_sample()
		{
			dict = new Dictionary();
			obj = {};
			arr = [];
			
			dict[obj] = "Letters";
			dict["obj"] = "Letters";
			dict[arr] = "Letters";
			trace(obj in dict);  // true
			trace("obj" in dict);  // true
			trace(arr in dict);  // true
			trace(dict[obj] == dict["obj"]);  // true
			
			var key:Object = {};
			dict["key"] = "Letters";
			obj["key"] = "Letters";
			key.toString = function():String { return "key"; }
			trace(obj[key] == "Letters"); // true because key == "key" is true b/c key.toString == "key"
			trace(dict[key] == "Letters"); // false because key === "key" is false
			//Dictionary对象中的键得到很好保存，可以是一个对象，而Object对象的键只可以是字符串，如果是对象就会 toString()。
			
			/*Object keys and memory management*/
			
			/*To make the object referenced by myObject eligible for garbage collection, you must remove
			all references to it. In this case, you must change the value of myObject and delete the myObject
			key from myMap, as shown in the following code:*/
			
			var _obj:Object = {};
			var _dict:Dictionary = new Dictionary();
			_dict[_obj] = "_obj";
			_obj = null;
			trace(_obj);  //null
			trace(_dict[_obj]); //undefined
			delete _dict[_obj];
			trace(_obj);  //null
			trace(_dict[_obj]); //undefined
			
			//仍然存在
			for (var _key:* in _dict)
			{
				trace(_key);  //[object Object]
				trace(_dict[_key]);  //_obj
			}
			//才能彻底删除
			for (var __key:* in _dict)
			{
				delete _dict[__key];
				__key = null;
				trace(__key);  //null
				trace(_dict[__key]);  //undefined
			}
			
			//由于删除光键，所以直接跳出循环
			for (var ___key:* in _dict)
			{
			}
			
			//颠倒delete _dict[__key];和__key = null;顺序，则不能删除键
			/*for (var __key:* in _dict)
			{
				__key = null;
				delete _dict[__key];
				trace(__key);  //null
				trace(_dict[__key]);  //undefined
			}
			
			for (var ___key:* in _dict)
			{
				trace(___key);  //[object Object]
				trace(_dict[___key]);  //_obj
			}*/
			   
			/*Alternatively, you can use the useWeakReference parameter of the Dictionary constructor to make 
			all of the dictionary keys weak references. The garbage collection system ignores weak references, 
			which means that an object that has only weak references is eligible for garbage collection. For 
			example, in the following code, you do not need to delete the myObject key from myMap in order to 
			make the object eligible for garbage collection:*/
			var obj_:Object = {};
			var dict_:Dictionary = new Dictionary(true);
			dict_[obj_] = "obj_";
			delete dict_[obj_];
			obj_ = null; // Make object eligible for garbage collection.
			
			//验证删除键值
			for (var key_:* in dict_)
			{
				trace(key_); //null
				trace(dict_[key_]);  //undefined
			}
			
			var hash:HashMap = new HashMap();
			var objs:Object = {};
			hash.add(objs,"objs");
			hash.removeKey(objs);
			  
			var object:Object = {};
			var dictionary:Dictionary = new Dictionary();
			dictionary[object] = "text_object";
			if(object in dictionary)
			{
				trace("find key");
			}
			//先删除值，再删除键
			delete dictionary[object];
			/*dictionary[object] = "other";
			trace(dictionary[object]);*/  //other
			object = null;
			trace(dictionary[object]); //undefined
			trace(object);
			
			//不会循环
			for (var keys:* in dictionary)
			{
				trace(keys);
				trace(dictionary[keys]);
			}
		}
	}
}